﻿using Service.Auth.Domain.Events;
using Service.Auth.Model.Enum;
using Service.Framework.Utils.Helper;
using Service.Repository;
using SqlSugar;


namespace Service.Auth.Domain.AggregatesModel
{
    /// <summary>
    /// 用户聚合
    /// </summary>
    [SugarTable("tb_user")]
    public class User : Entity<long>, IAggregateRoot, IEntityTenant
    {
        public User(string account, PasswordValueObject passwordValueObject, string realName, string phone)
        {
            Id = SnowflakeUtil.Instance().GetId();
            Account = account;
            PasswordValueObject = passwordValueObject;
            RealName = realName;
            Phone = phone;
            NeedUpPwd = true;
            Status = UsetStatusEnum.Normal;
        }

        public User()
        {
        }

        #region 属性

        /// <summary>
        /// 登陆账号 
        ///</summary>
        [SugarColumn(ColumnName = "account")]
        public string Account { get; private set; }

        /// <summary>
        /// 登陆密码  值对象
        ///</summary>
        [SugarColumn(IsOwnsOne = true)]
        public PasswordValueObject PasswordValueObject { get; set; }


        // /// <summary>
        // /// 密码盐 
        // ///</summary>
        // [SugarColumn(ColumnName = "salt")]
        // public string Salt { get; private set; }

        /// <summary>
        /// 名称 
        ///</summary>
        [SugarColumn(ColumnName = "real_name")]
        public string RealName { get; set; }

        public void SetRealName(string name)
        {
            RealName = name;
        }


        /// <summary>
        /// 联系方式 
        ///</summary>
        [SugarColumn(ColumnName = "phone")]
        public string Phone { get; set; }


        /// <summary>
        /// 乐观锁版本标识
        ///</summary>
        [SugarColumn(IsEnableUpdateVersionValidation = true, ColumnName = "version")]
        public long Version { get; private set; }


        [SugarColumn(ColumnName = "tenant_id")]
        public string TenantId { get; set; }

        /// <summary>
        /// 需要修改密码
        /// </summary>
        [SugarColumn(ColumnName = "need_up_pwd")]
        public bool NeedUpPwd { get; private set; }


        [Navigate(NavigateType.OneToMany, nameof(UserSystemRole.UserId))]
        public List<UserSystemRole> UserSystemRoles { get; private set; }


        /// <summary>
        /// 头像图片
        /// </summary>
        [SugarColumn(ColumnName = "picture")]
        public string Picture { get; private set; }


        [SugarColumn(ColumnName = "status")]
        public UsetStatusEnum Status { get; private set; }

        #endregion

        #region 事件

        public void SetAccount(string value)
        {
            Account = value;
        }
        public void SetPicture(string value)
        {
            Picture = value;
        }

        /// <summary>
        /// 用户禁用
        /// </summary>
        /// <param name="requestId"></param>
        /// <param name="code"></param>
        public void SetUserStatus(UsetStatusEnum usetStatus)
        {
            Status = usetStatus;
            AddDomainEvent(new UserChangeStatusDomainEvent(Id, Status));
        }


        public void ChangePassword(string value)
        {
            var passwordValueObject = new PasswordValueObject(value);
            PasswordValueObject = passwordValueObject;
            NeedUpPwd = false;
            //修改密码事件
            AddDomainEvent(new ChangePasswordDomainEvent(Id, RealName));
        }


        /// <summary>
        /// 设置用户所属系统及角色
        /// </summary>
        /// <param name="systemsRoleIds"></param>
        public void SetSystemRoles(List<long> systemIds, List<string> roleNum)
        {
            AddDomainEvent(new UserAddSystemRolesDomainEvent(Id, systemIds, roleNum));
        }

        /// <summary>
        /// 手机号验证
        /// </summary>
        /// <param name="requestId"></param>
        /// <param name="code"></param>
        public void ChangePhone(string value)
        {
            AddDomainEvent(new ChangePhoneDomainEvent(Id, value));
            Phone = value;
        }
        #endregion
    }
}